home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / FGFONT10.ZIP / FONTDEMO.C < prev    next >
Text File  |  1991-06-03  |  13KB  |  443 lines

  1. /**********************************************************************\
  2.                                                                      
  3. Fontdemo program -- this is a Fastgraph demo program by Diana Gruber 
  4. I created the font file 14pt.fnt in my favorite paint program (dPaint
  5. II enhanced) and used the SNAPSHOT utility to capture it into a packed 
  6. pixel run format.  This program displays the font file and then uses 
  7. the fg_getmap funtion to store the  characters in a mode-independent 
  8. bitmap format.  Normally you would want to do the "get_font" function 
  9. on an off-screen page.
  10.  
  11. I included some routines to display the characters. Feel free to 
  12. incorporate any of this code in your programs, and give me a call if 
  13. you need technical support. The technical support number is 
  14. (702) 735-1980.                      
  15.  
  16. to compile and link (Microsoft C):
  17.  
  18.    cl /c /W3 /AM fontdemo.c
  19.    link /E fontdemo,fontdemo.exe,NUL.MAP,fgm
  20.                                                                       
  21. \**********************************************************************/
  22.  
  23. #include "fontdemo.h"
  24.  
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <malloc.h>
  28. #include <fastgraf.h>
  29. #include <string.h>
  30. #include <sys/types.h>
  31. #include <sys/stat.h>
  32.  
  33. #define FALSE 0
  34. #define TRUE  1
  35. #define PTSIZE 14
  36.  
  37. int old_mode;
  38. int hidden;
  39. int visual;
  40.  
  41. char *font;
  42. char *pfont;
  43.  
  44. /* spacing for 14 point proportional spaced characters */
  45.  
  46. int spacing[] = {
  47. /* !  "  #  $  %  &  '  (  )  *  +  ,  - */
  48.    4, 7, 9, 8,13, 9, 4, 5, 5, 8, 8, 4, 6,
  49.  
  50. /* .  /  0  1  2  3  4  5  6  7  8  9  : */
  51.    4, 5, 8, 6, 8, 8, 8, 8, 8, 8, 8, 8, 4,
  52.  
  53. /* ;  <  =  >  ?  @  A  B  C  D  E  F  G */
  54.    4, 8, 8, 8, 8,13,10,10, 9,10, 9, 9,10,
  55.  
  56. /* H  I  J  K  L  M  N  O  P  Q  R  S  T */
  57.   10, 4, 7, 9, 9,12,10,10,10,10,11, 9,10,
  58.  
  59. /* U  V  W  X  Y  Z  [  \  ]  ^  _  `  a */
  60.   10,10,16,10,12,11, 5, 6, 5, 7, 9, 6, 8,
  61.  
  62. /* b  c  d  e  f  g  h  i  j  k  l  m  n */
  63.    8, 8, 8, 8, 5, 8, 8, 4, 4, 8, 4,12, 8,
  64.  
  65. /* o  p  q  r  s  t  u  v  w  x  y  z  { */
  66.    8, 8, 8, 7, 8, 5, 8,10,12,10,10, 8, 6,
  67.  
  68. /* |  }  ~ */
  69.    5, 6,12};
  70.  
  71. /**********************************************************************\
  72. *                                                                      *
  73. *                                 main                                 *
  74. *                                                                      *
  75. \**********************************************************************/
  76.  
  77. void main()
  78. {
  79.    unsigned char key,aux;
  80.  
  81.    if (fg_egacheck() != 4) 
  82.    {
  83.       printf("\n");
  84.       printf("This program requires an EGA with 256K \n");
  85.       printf("and an Enhanced Color Display (ECD).\n\n");
  86.       printf("If an EGA is present, it must be the active adapter.\n");
  87.       exit(0);
  88.    }
  89.  
  90.    init_graphics(16);
  91.  
  92.    fg_setpage(visual);
  93.    get_font();
  94.    fg_setcolor(15);
  95.    center_pstring("Press any key to continue",0,639,348);
  96.    fg_getkey(&key,&aux);
  97.    if (key == 27)
  98.       quit_graphics();
  99.  
  100.    fg_setcolor(1);
  101.    fg_rect(0,639,0,349);
  102.  
  103.    fg_setcolor(15);
  104.    put_pstring("Proportional spaced 14 point font",10,20);
  105.    put_pstring("The quick brown fox jumped over the lazy dog.",10,35);
  106.    put_pstring("Don't you think this is an attractive font?",10,50);
  107.  
  108.    put_fstring("Fixed pitch 14 point font",10,80);
  109.    put_fstring("Convenient for data entry",10,95);
  110.  
  111.  
  112.    put_tstring("ROM text (hardware) font",8,1);
  113.    put_tstring("Characters displayed in fixed character cells",9,1);
  114.  
  115.    fg_waitkey();
  116.    quit_graphics();
  117. }
  118.  
  119. /**********************************************************************\
  120. *                                                                      *
  121. *      abort_program -- called when the program can't be run           *
  122. *                                                                      *
  123. \**********************************************************************/
  124.  
  125. void abort_program()
  126. {
  127.    fg_setmode(old_mode);
  128.    fg_reset();
  129.    printf("\nUnable to execute program.\n");
  130.    exit(0);
  131. }
  132.  
  133. /**********************************************************************\
  134. *                                                                      *
  135. *  center_pstring -- display proportional characters, centered         *
  136. *                                                                      *
  137. \**********************************************************************/
  138.  
  139. void center_pstring(string,x1,x2,y)
  140. char *string;
  141. int x1, x2, y;
  142. {
  143.    int x;
  144.  
  145.    x = get_center(string,x1,x2);
  146.    put_pstring(string,x,y);
  147. }
  148.    
  149. /**********************************************************************\
  150. *                                                                      *
  151. *  center_string -- display fixed-pitch characters, centered           *
  152. *                                                                      *
  153. \**********************************************************************/
  154.  
  155. void center_string(string,x1,x2,y)
  156. char *string;
  157. int x1, x2, y;
  158. {
  159.    int nchar;
  160.    int x;
  161.  
  162.    nchar = strlen(string);
  163.    if (nchar == 0) return;
  164.  
  165.    x = ((x1 + x2) / 2) - (nchar * 4);
  166.    put_fstring(string,x,y);
  167. }
  168.  
  169. /**********************************************************************\
  170. *                                                                      *
  171. *                   exists -- does the file exist?                     *       
  172. *                                                                      *
  173. \**********************************************************************/
  174.  
  175. exists(filename)
  176. char *filename;
  177. {
  178.    struct stat buf;
  179.    int status;
  180.  
  181.    status = stat(filename,&buf);
  182.  
  183.    if (status == 0)
  184.       return(TRUE);
  185.    else
  186.       return(FALSE);
  187. }
  188.  
  189. /**********************************************************************\
  190. *                                                                      *
  191. *         final_message  -- colorful exit screen                       *
  192. *                                                                      *
  193. \**********************************************************************/
  194.  
  195. void final_message()
  196. {
  197.    int color;
  198.    register int column;
  199.  
  200.    /* some green rectangles */
  201.  
  202.    fg_setcolor(10);
  203.    fg_rect(0,79,0,4);
  204.    fg_setcolor(2);
  205.    fg_rect(2,77,1,3);
  206.  
  207.    /* display the string in a single color */
  208.  
  209.    fg_setattr(15,2,0);
  210.    put_tstring("FONTDEMO program by Diana Gruber",2,25);
  211.  
  212.    /* change the colors of all the letters */
  213.  
  214.    color = 9;
  215.    fg_locate(2,25);
  216.    for (column = 25; column < 57; column++)
  217.    {
  218.       fg_setattr(color,2,0);
  219.       fg_chgattr(1);
  220.       color++;
  221.       if (color > 15) color = 9;
  222.    }
  223.  
  224.    fg_locate(5,0);
  225.    return;
  226. }
  227.  
  228. /**********************************************************************\
  229. *                                                                      *
  230. *  get_center -- locate center for proportionally-spaced characters    *
  231. *                                                                      *
  232. \**********************************************************************/
  233.  
  234. get_center(string,x1,x2)
  235. char *string;
  236. int x1, x2;
  237. {
  238.    return(((x1 + x2) / 2) - (length_pstring(string)/ 2));
  239. }
  240.  
  241. /**********************************************************************\
  242. *                                                                      *
  243. *  get_font -- get the proportional and fixed-pitch character fonts    *
  244. *                                                                      *
  245. \**********************************************************************/
  246.  
  247. void get_font()
  248. {
  249.    register int i;
  250.    register int index;
  251.    int x, y;
  252.    int yoffset;
  253.    char fontfile[10];
  254.  
  255.    /* allocate dynamic memory for the fonts; also define font file name */
  256.  
  257.    font  = malloc(1316);
  258.    pfont = malloc(2632);
  259.    strcpy(fontfile,"14pt.ppr");
  260.    yoffset = 18;
  261.  
  262.    /* if the allocate failed or the font file isn't present, go no further */
  263.  
  264.    if (font  == (char *)NULL) abort_program();
  265.    if (pfont == (char *)NULL) abort_program();
  266.    if (!exists(fontfile))     abort_program();
  267.  
  268.    /* display the 14pt fonts on the hidden page */
  269.  
  270.    fg_move(0,199);
  271.    fg_dispfile(fontfile,320,1);
  272.  
  273.    /* retrieve the proportionally spaced and fixed-pitch fonts */
  274.  
  275.    fg_setcolor(11);
  276.    index = 0;
  277.  
  278.    for (i = 0; i < 94; i++)
  279.    {
  280.       x = 1 + (i/13) * 16;
  281.       y = yoffset + (i%13) * (PTSIZE+1);
  282.       fg_move(x,y);
  283.       fg_getmap(&pfont[index<<1],2,PTSIZE);
  284.       x = 129 + (i/13) * 9;
  285.       fg_move(x,y);
  286.       fg_getmap(&font[index],1,PTSIZE);
  287.       index += PTSIZE;
  288.    }
  289. }
  290.  
  291. /**********************************************************************\
  292. *                                                                      *
  293. *      init_graphics -- initialize the graphics environment            *
  294. *                                                                      *
  295. \**********************************************************************/
  296.  
  297. void init_graphics(mode)
  298. int mode;
  299. {
  300.  
  301.    /* find out what the old video mode is */
  302.  
  303.    old_mode = fg_getmode();
  304.  
  305.    /* new mode is hi-res EGA (640X350) */
  306.  
  307.    fg_setmode(mode);
  308.  
  309.    /* set up visual and hidden pages */
  310.  
  311.    fg_setpage(0);
  312.    fg_setvpage(0);
  313.    fg_sethpage(1);
  314.    visual = 0;
  315.    hidden = 1;   
  316. }
  317.  
  318. /**********************************************************************\
  319. *                                                                      *
  320. *  length_pstring -- compute the length of a proportional string       *
  321. *                                                                      *
  322. \**********************************************************************/
  323.  
  324. length_pstring(string)
  325. char *string;
  326. {
  327.    register int i;
  328.    register int nchar;
  329.    int length;
  330.    char ch;
  331.  
  332.    nchar = strlen(string);
  333.    if (nchar == 0) return(0);
  334.  
  335.    length = 0;
  336.  
  337.    for (i = 0; i < nchar; i++)
  338.    {
  339.       ch = string[i] - 33;
  340.       if (ch == -6) /* this is ESC (27-33 = -6) */
  341.          i += 2;
  342.       else if (ch < 0)
  343.          length += 8;
  344.       else
  345.          length += spacing[ch];
  346.    }
  347.  
  348.    return(length);
  349. }
  350.  
  351. /**********************************************************************\
  352. *                                                                      *
  353. *  put_fstring -- display fixed-pitch characters                       *
  354. *                                                                      *
  355. \**********************************************************************/
  356.  
  357. void put_fstring(string,x,y)
  358. char *string;
  359. int x, y;
  360. {
  361.    register int i;
  362.    register int nchar;
  363.    int index;
  364.    char ch;
  365.    
  366.    nchar = strlen(string);
  367.    if (nchar == 0) return;
  368.  
  369.    for (i = 0; i < nchar; i++)
  370.    {
  371.       ch = string[i] - 33;
  372.       if (ch >= 0)
  373.       {
  374.          index = ch * PTSIZE;
  375.          fg_move(x,y);
  376.          fg_drawmap(&font[index],1,PTSIZE);
  377.       }
  378.       x += 8;
  379.    }
  380. }
  381.  
  382. /**********************************************************************\
  383. *                                                                      *
  384. *  put_pstring -- display proportionally-spaced characters             *
  385. *                                                                      *
  386. \**********************************************************************/
  387.  
  388. void put_pstring(string,x,y)
  389. char *string;
  390. int x, y;
  391. {
  392.    register int i;
  393.    register int nchar;
  394.    int index;
  395.    char ch;
  396.  
  397.    nchar = strlen(string);
  398.    if (nchar == 0) return;
  399.  
  400.    for (i = 0; i < nchar; i++)
  401.    {
  402.       ch = string[i] - 33;
  403.       if (ch >= 0)
  404.       {
  405.          index = ch * PTSIZE * 2;
  406.          fg_move(x,y);
  407.          fg_drawmap(&pfont[index],2,PTSIZE);
  408.          x += spacing[ch];
  409.       }
  410.       else
  411.          x += 8;
  412.    }
  413. }
  414.  
  415. /**********************************************************************\
  416. *                                                                      *
  417. *  put_tstring -- display ROM BIOS text                                *
  418. *                                                                      *
  419. \**********************************************************************/
  420.  
  421. void put_tstring(string,row,col)
  422. char *string;
  423. int row, col;
  424. {
  425.    fg_locate(row,col);
  426.    fg_text(string,strlen(string));
  427. }
  428.  
  429. /**********************************************************************\
  430. *                                                                      *
  431. *    quit_graphics -- return the computer to its original state        *
  432. *                                                                      *
  433. \**********************************************************************/
  434.  
  435. void quit_graphics()
  436. {
  437.    fg_setmode(old_mode);
  438.    fg_reset();
  439.    final_message();
  440.    exit(0);
  441. }
  442.  
  443.